package recharge

import (
	"context"
	"strconv"
	"strings"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/consts"
	"gitee.com/liuxuezhan/ar-platform/rpc-client/assetsClient/rpc"
	"github.com/zeromicro/go-zero/core/logc"

	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/svc"
	"gitee.com/liuxuezhan/ar-platform/assets-api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type RechargeChannelListLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// 用户充值页面--充值通道--列表
func NewRechargeChannelListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RechargeChannelListLogic {
	return &RechargeChannelListLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *RechargeChannelListLogic) RechargeChannelList(req *types.RechargeChannelListRequest) (resp []types.RechargeChannelListResponse_Data, err error) {
	var res = make([]types.RechargeChannelListResponse_Data, 0)
	data, err := l.svcCtx.AssetsRpc.PayRecharge.RechargeChannelList(l.ctx, &rpc.RechargeChannelListRequest{
		Type:     uint32(req.RechargeTypeId),
		Status:   consts.RechargeChannelStatusOpen,
		Page:     1,
		PageSize: 1000,
	})

	if data != nil {
		for i := 0; i < len(data.Records); i++ {
			amountSp := strings.Split(data.Records[i].AmountConfig, "|")
			var amountList []float64
			for i := 0; i < len(amountSp); i++ {
				item, err := strconv.ParseFloat(amountSp[i], 64)
				if err != nil {
					logc.Error(l.ctx, err)
					continue
				}
				amountList = append(amountList, item)
			}

			res = append(res, types.RechargeChannelListResponse_Data{
				Id:         data.Records[i].Id,
				Name:       data.Records[i].Name,
				BonusRate:  data.Records[i].BonusRate,
				MinAmount:  data.Records[i].MinAmount,
				MaxAmount:  data.Records[i].MaxAmount,
				AmountList: amountList,
				Icon:       data.Records[i].Icon,
				CoinType:   data.Records[i].Inr,
				Rate:       data.Records[i].ToRate,
			})
		}
	}

	return res, nil
}
